package com.extremelylargepackage;

import com.extremelylargepackage.SchmupView.EngineType;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity
{
	private SchmupView _view;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		Bundle level = getIntent().getExtras();
		String levelName = level.getString("level");
		boolean local = level.getBoolean("local");
		_view = new SchmupView(this, null);
		setContentView(_view);
		_view.getThread().startGameEngine(levelName, local);
	}
	
    @Override
	public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
    @Override
    public void onPause()
    {
    	super.onPause();
    	_view.getThread().pauseGame();
    	_view.getThread().setVisible(false);
    }

    @Override
    public void onStop() {
    	super.onStop();
    	//TODO: we should get this working eventually. Its causing crashes for now.
    	//_view.getThread().saveGameState();
    }

    @Override
    public void onResume()
    {
    	super.onResume();
    	_view.getThread().resumeGame();
    	_view.getThread().setVisible(true);
    }
    
    @Override
	public void onDestroy()
    {
    	super.onDestroy();
    	_view.getThread().setRunning(false);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    	if(keyCode == KeyEvent.KEYCODE_BACK)
    	{
    		onPause();
    		new AlertDialog.Builder(this).setMessage("Exit game?")
    			.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
    			{
					@Override
					public void onClick(DialogInterface dialog, int which) 
					{
						_view.getThread().setRunning(false);
						finish();
					}
				})
				.setNegativeButton("No", new DialogInterface.OnClickListener() 
				{
					@Override
					public void onClick(DialogInterface dialog, int which) 
					{
						onResume();
					}
				}).show();
    		return true;
    	}
    	return super.onKeyDown(keyCode, event);
    }
}
